/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.mto.quangcao.service.impl;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.repository.model.Folder;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.MimeTypesUtil;

import com.liferay.portal.model.Role;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portlet.documentlibrary.model.DLFolder;
import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
import com.mto.quangcao.model.QuangCao_banner;
import com.mto.quangcao.service.base.QuangCao_bannerLocalServiceBaseImpl;
import com.mto.quangcao.service.persistence.QuangCao_bannerFinder;
import com.mto.quangcao.service.persistence.QuangCao_bannerFinderUtil;
import com.mto.quangcao.service.persistence.QuangCao_bannerUtil;

/**
 * The implementation of the quang cao_banner local service.
 * 
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link com.mto.quangcao.service.QuangCao_bannerLocalService} interface.
 * 
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 * 
 * @author thanh.tran
 * @see com.mto.quangcao.service.base.QuangCao_bannerLocalServiceBaseImpl
 * @see com.mto.quangcao.service.QuangCao_bannerLocalServiceUtil
 */
public class QuangCao_bannerLocalServiceImpl extends
		QuangCao_bannerLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 * 
	 * Never reference this interface directly. Always use {@link
	 * com.mto.quangcao.service.QuangCao_bannerLocalServiceUtil} to access the
	 * quang cao_banner local service.
	 */
	public void create(QuangCao_banner quangCao) throws SystemException {

		QuangCao_banner qc = QuangCao_bannerUtil.create(CounterLocalServiceUtil
				.increment(QuangCao_banner.class.getName()));

		qc.setContent(quangCao.getContent());
		qc.setCountClick(quangCao.getCountClick());
		qc.setCountView(quangCao.getCountView());
		qc.setUserId(quangCao.getUserId());
		qc.setStartDate(quangCao.getStartDate());
		qc.setEndDate(quangCao.getEndDate());
		qc.setTitle(quangCao.getTitle());
		qc.setDescription(quangCao.getDescription());
		qc.setFileEntryId(quangCao.getFileEntryId());
		qc.setStatus(quangCao.getStatus());
		qc.setType(quangCao.getType());
		qc.setCreateDate(quangCao.getCreateDate());
		qc.setLink(quangCao.getLink());
		
		qc.setUserModifyId(quangCao.getUserModifyId());
		qc.setUserModifyIp(quangCao.getUserModifyIp());
		qc.setWidthBanner(quangCao.getWidthBanner());
		qc.setHeightBanner(quangCao.getHeightBanner());
		quangCao_bannerPersistence.update(qc, true);
		
		System.out.println("tao quang cao thanh cong !");
	}

	public void edit(QuangCao_banner quangCao) {
		try {
			quangCao_bannerPersistence.update(quangCao, true);
			System.out.println("Cap nhat thanh cong !");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public boolean checkRoleKhachHang(ThemeDisplay themeDisplay) {
		boolean flag = false;

		try {
			List<Role> role = RoleLocalServiceUtil.getRoles(themeDisplay
					.getCompanyId());

			for (int i = 0; i < role.size(); i++) {
				if (role.get(i).getCompanyId() == themeDisplay.getCompanyId()
						&& role.get(i).getName().equals("KhachHang_QuangCao")) {
					flag = true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	public long UploadFile(String nameFolder,
			UploadPortletRequest uploadRequest, ThemeDisplay themeDisplay,
			ServiceContext serviceContext) {
		long fileEntryID = 0;
		try {
			File file = (File) uploadRequest.getFile("fileName");
			System.out.println("Ten File" + file.getPath());
			long folderId;
			long repositoryId;
			if (checkFolder(themeDisplay, nameFolder) == false) {
				Folder folder = createFolder(themeDisplay, serviceContext,
						nameFolder);
				folderId = folder.getFolderId();
				repositoryId = folder.getRepositoryId();
				System.out.println("Lay Folder ID thang cong" + folderId);
			} else {
				folderId = getFolderID(themeDisplay, nameFolder);
				repositoryId = get_repositoryId(themeDisplay, nameFolder);
				System.out.println("repositoryId :" + repositoryId);
			}
			if (file.exists()) {
				fileEntryID = DLAppLocalServiceUtil.addFileEntry(
						themeDisplay.getUserId(), repositoryId, folderId,
						file.getName(), MimeTypesUtil.getContentType(file),
						file.getName(), "", "", file, serviceContext)
						.getFileEntryId();

				System.out.println("Up load file Thanh cong");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileEntryID;
	}

	public boolean checkFolder(ThemeDisplay theme, String nameFolder) {
		boolean flag = false;
		try {
			List<DLFolder> folder = DLFolderLocalServiceUtil.getFolders(
					theme.getPortletGroupId(), Long.parseLong("0"));
			for (int i = 0; i < folder.size(); i++) {
				
				if (folder.get(i).getName().equals(nameFolder)
						|| folder.get(i).getFolderId() == theme
								.getCompanyGroupId()) {
					flag = true;
					System.out.println("Ton tai Folder ID");
					return flag;
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	public Folder createFolder(ThemeDisplay themeDisplay,
			ServiceContext serviceContext, String nameFolder) {

		Folder folder = null;
		if (checkFolder(themeDisplay, nameFolder) == false) {
			try {
				System.out.println("Tao folder!");
				long repositoryId = themeDisplay.getScopeGroupId();
				System.out.println("repositoryId " + repositoryId);
				long parentFolderId = 0l;
				System.out.println("parentFolderId " + parentFolderId);
				System.out.println("themeDisplay.getUserId() "
						+ themeDisplay.getUserId());
				System.out.println("nameFolder " + nameFolder);

				folder = DLAppLocalServiceUtil.addFolder(
						themeDisplay.getUserId(), repositoryId, parentFolderId,
						nameFolder, nameFolder, serviceContext);
				System.out.println("Tao folder thanh cong !");
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("Tao folder that bai !");
			}
		}

		return folder;
	}

	public long getFolderID(ThemeDisplay theme, String nameFolder) {
		long folderID = 0;
		try {
			List<DLFolder> folder = DLFolderLocalServiceUtil.getFolders(
					theme.getPortletGroupId(), Long.parseLong("0"));
			for (int i = 0; i < folder.size(); i++) {
				if (folder.get(i).getName().equals(nameFolder)) {
					folderID = folder.get(i).getFolderId();
					return folderID;
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return folderID;
	}

	public long get_repositoryId(ThemeDisplay theme, String nameFolder) {
		long repositoryId = 0;
		try {
			List<DLFolder> folder = DLFolderLocalServiceUtil.getFolders(
					theme.getPortletGroupId(), Long.parseLong("0"));
			for (int i = 0; i < folder.size(); i++) {
				if (folder.get(i).getName().equals(nameFolder)) {
					repositoryId = folder.get(i).getRepositoryId();
					return repositoryId;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return repositoryId;
	}
	
	public List<QuangCao_banner> getALL(long quangCaoId , String type, String title)
	{
		return QuangCao_bannerFinderUtil.getAll(quangCaoId, type, title);
	}
	
}